<?php
namespace Home\Controller;
use Think\Controller;
header('Content-Type:text/html;charset=utf-8');
class ProjectController extends AuthController{
    public function index(){
        $indexes = $_GET['Indexes'];
        $proc = M('project_category')->where('project_category_name =\''.$indexes.'\'')->select();
        $pid = $proc['0']['id'];
        $post = I('post.search');
        $map['pro_name'] = array('like',"%$post%");
        //dump($post);
        $pro = M('project');
        if(!empty($indexes)){
            $count = $pro->where('pro_examine = 1 AND pro_state != 1 AND project_category_id='.$pid)->count();
        }else if(!empty($post)){
            $count = $pro->where($map)->where('pro_examine = 1 AND pro_state != 1')->count();
        }else{
            $count = $pro->where('pro_examine = 1 AND pro_state != 1')->count();
        }
        $Page = new \Think\Page($count,3);
        //dump($Page);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');

        $show  =  $this->bootstrap_page_style($Page->show());
        $procate = M('project_category')->select();
        $value = session('home_user');
        //dump($show);
        $user = M('user')->select();

        if(!empty($indexes)){
            $rules = $pro->where('pro_examine = 1 AND pro_state != 1 AND project_category_id='.$pid)->limit($Page->firstRow.','.$Page->listRows)->select();
        }else if(!empty($post)){
            $rules = $pro->where($map)->where('pro_examine = 1 AND pro_state != 1')->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $rules = $pro->where('pro_examine = 1 AND pro_state != 1')->limit($Page->firstRow.','.$Page->listRows)->select();
        }

        $this->assign('user' ,$user);
        $this->assign('pro',$rules);
        $this->assign('page',$show);
        $this->assign('procate',$procate);
        $this->assign('value',$value);
		$this->display('project');
    }
	
	public function project_release(){
        $id = $_GET['id'];
        $ur = D('user')->find($id);
        $procate = D('project_category')->select();
        //dump($ur);
        $value = session('home_user');
        if(empty($value)){
            $this->error('请先登录账号',U('login/index'));
        }
        if(IS_POST){
            $post = I('post.');
            $post['user_id'] = session('home_user')['id'];
            $post['create_time'] = time();
            //dump($post);exit;
            $pro = D('project');
            if(!empty($post['pro_name']) && !empty($post['pro_type_id']) && !empty($post['project_category_id']) && !empty($post['pro_budget']) && !empty($post['pro_content'])){
                if($pro->create()){
                    if($pro->add($post)){
                        $this->success('发布成功',U('project/project_release'));
                    }else{
                        $this->error('发布失败');
                    }
                }else{
                    $this->error($pro->getError());
                }
            }else{
                $this->error('请输入内容',U('project/project_release'));
            }
        }
        $this->assign('procate' ,$procate);
        $this->display('project_release');
	}
	
	public function project_details(){
        $id = $_GET['id'];
	    $project = M('project')->find($id);
	    //dump($project);
	    $this->assign('project' , $project);
	    $this->assign('get' , $_GET);
		$this->display('project_details');
	}

	public function ajax(){
	    if(IS_POST){
	        $pro  = M('project')->where('pro_id="'.I('post.pro_id').'"')->find();
	        $arr = explode(',',$pro['pro_applicant']);
	        if($pro['user_id'] != I('post.pro_applicant')){
                if(in_array(I('post.pro_applicant'),$arr)){
                    $this->ajaxReturn('已经您已经申请过了');
                }else{
                    $arr[] = I('post.pro_applicant');
                    $str = implode(',',$arr);
                    $string = trim($str,',');
                    $time = time();
                    if(M('project')->where('pro_id="'.I('post.pro_id').'"')->save(array('pro_applicant'=>$string))){    //修改项目申请信息
                        if(M('message')->add(array('mes_sender'=>I('post.pro_applicant') , 'mes_pro'=>I('post.pro_id'),'mes_title'=>'项目申请','mes_content'=>$pro['pro_name'],'mes_time'=>$time))){//建立新的消息信息
                            $mes_id = M('message')->where('mes_sender="'.I('post.pro_applicant').'" AND mes_time="'.$time.'"')->find();
                            $user = M('user')->where('id="'.$pro['user_id'].'"')->find();
                            if(M('user')->where('id="'.$pro['user_id'].'"')->save(array('mes_id'=>$user['mes_id'].','.$mes_id['mes_id']))){
                                $this->ajaxReturn('申请成功');
                            }
                        }
                    }
                }
            }else{
                $this->ajaxReturn('自己不能申请自己发布的项目');
            }
        }
    }


	public function publisher(){
        $id = $_GET['id'];
        $user = M('user')->find($id);
		//dump($user);
		$degree = M('degree')->select();
		$this->assign('degree' ,$degree);
        $this->assign('user' ,$user);
	    $this->display('publisher');
    }

    public function publishedProject(){
        $id = $_GET['id'];
        $pro = M('project');
        $count = $pro->where('user_id ='.$id)->count();
        $Page = new \Think\Page($count,3);
        //dump($Page);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');

        $show  =  $this->bootstrap_page_style($Page->show());
        $project = $pro->where('user_id ='.$id)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('id' ,$id);
        $this->assign('page',$show);
        $this->assign('project',$project);
        $this->display('publishedProject');
    }

}